using Autofac;
using Autofac.Extensions.DependencyInjection;
using Global.Validation;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.EntityFrameworkCore;
using Microsoft.Office.Interop.Excel;
using ProdService;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(
   builder =>
   {
       builder.RegisterAssemblyTypes(typeof(BaseService).Assembly).AsSelf();
   });

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddSession();
builder.Services.AddDistributedMemoryCache();

builder.Services.AddHttpContextAccessor();

builder.Services.AddScoped<DbContext, MysqlEFConfig.MysqlDbContext>();
//builder.Services.AddTransient<BaseService, BaseService>();
builder.Services.AddSingleton<IClientModelValidator, AtLeastOneRequired>();


var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseSession();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
      name: "areas",
      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
    );
});

app.MapControllerRoute(
    name: "UserHomepage",
    pattern: "User/{id:int?}",
    defaults: new { controller = "User", action = "HomePage" });

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
